package com.example.fourtestapp.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.fourtestapp.R;
import com.example.fourtestapp.entity.MilieuInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MilieuFragment extends Fragment {

    private TextView yuZhiBtn;

    private LinearLayout tempContent;
    private TextView tempTitle;
    private TextView tempNum;
    private LinearLayout sdContent;
    private TextView sdTitle;
    private TextView sdNum;
    private LinearLayout lightContent;
    private TextView lightTitle;
    private TextView lightNum;
    private LinearLayout co2Content;
    private TextView co2Title;
    private TextView co2Num;
    private LinearLayout pmContent;
    private TextView pmTitle;
    private TextView pmNum;
    private LinearLayout roadContent;
    private TextView roadTitle;
    private TextView roadNum;

    private MilieuInfo info;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_milieu, container, false);
        initView(view);
        initListener();

        EventBus.getDefault().register(this);
        return view;
    }

    private void initView(View view) {
        tempContent = view.findViewById(R.id.temp_content);
        tempTitle = view.findViewById(R.id.temp_title);
        tempNum = view.findViewById(R.id.temp_num);
        sdContent = view.findViewById(R.id.sd_content);
        sdTitle = view.findViewById(R.id.sd_title);
        sdNum = view.findViewById(R.id.sd_num);
        lightContent = view.findViewById(R.id.light_content);
        lightTitle = view.findViewById(R.id.light_title);
        lightNum = view.findViewById(R.id.light_num);
        co2Content = view.findViewById(R.id.co2_content);
        co2Title = view.findViewById(R.id.co2_title);
        co2Num = view.findViewById(R.id.co2_num);
        pmContent = view.findViewById(R.id.pm_content);
        pmTitle = view.findViewById(R.id.pm_title);
        pmNum = view.findViewById(R.id.pm_num);
        roadContent = view.findViewById(R.id.road_content);
        roadTitle = view.findViewById(R.id.road_title);
        roadNum = view.findViewById(R.id.road_num);
        yuZhiBtn = view.findViewById(R.id.yuZhi_btn);
    }

    private void initListener() {

        yuZhiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), MilieuSettingActivity.class));
            }
        });


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void GetData(MilieuInfo info) {
        this.info = info;
        if (info != null){
            tempNum.setText(info.getTemp() + "");
            sdNum.setText(info.getHumidity() + "");
            co2Num.setText(info.getCo2() + "");
            lightNum.setText(info.getLightIntensity() + "");
            pmNum.setText(info.get_$Pm2594() + "");
            roadNum.setText(info.getRoad() + "");
            if (info.getTemp() > 30) {
                tempContent.setBackgroundColor(Color.RED);
            } else {
                tempContent.setBackgroundColor(Color.GREEN);
            }
            if (info.getHumidity() > 30) {
                sdContent.setBackgroundColor(Color.RED);
            } else {
                sdContent.setBackgroundColor(Color.GREEN);
            }
            if (info.getLightIntensity() > 30) {
                lightContent.setBackgroundColor(Color.RED);
            } else {
                lightContent.setBackgroundColor(Color.GREEN);
            }
            if (info.getCo2() > 30) {
                co2Content.setBackgroundColor(Color.RED);
            } else {
                co2Content.setBackgroundColor(Color.GREEN);
            }
            if (info.get_$Pm2594() > 30) {
                pmContent.setBackgroundColor(Color.RED);
            } else {
                pmContent.setBackgroundColor(Color.GREEN);
            }
            if (info.getRoad() > 4) {
                roadContent.setBackgroundColor(Color.RED);
            } else {
                roadContent.setBackgroundColor(Color.GREEN);
            }
        }
    }
}
